<template>
	<om-formsub-outer :index="index" :checkedit="checkedit">
		<om-autoform ref="autoform" :initConfigData="autoformdata" :initValue="initValue"></om-autoform>
	</om-formsub-outer>
</template>

<script>
	import {buildSubFormData, subOnLoad,getEditorTitle, changeEditor,configSelectData,hasValue} from '@/static/js/formUtil.js';
	import { ignoreHtml,ptFloat,minusF,getDateStr} from '@/static/js/common.js';
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return buildSubFormData({
				indexCache:"obj-year-tinstAnnualTargetPlan-title_monthIndex-order-cache",
				index1Cache:"obj-year-tinstAnnualTargetPlan-title_monthIndex-order1-cache",
				indexList:[]
			})
		},
		methods:{
			verifyQuarter(id){
				let _that = this
				_that.om.request(
					"torgAnnualTargetPlan/verifyQuarter",
					{
						indexId: id
					}
				).then(res=>{
					if(!res.value){
						for(let item of _that.$refs.autoform.configData[0].editors){
							if(item.valueField.includes("quarter")){
								_that.$refs.autoform.changeEditor("title_monthIndex",item.valueField,"type","labelbox")
								_that.$refs.autoform.changeEditor("title_monthIndex",item.valueField,"suffix","")
							}
						}
					}else{
						for(let item of _that.$refs.autoform.configData[0].editors){
							if(item.valueField.includes("quarter")){
								_that.$refs.autoform.changeEditor("title_monthIndex",item.valueField,"type","numberbox")
							}
						}
					}
				})
			},
			verifySubConfirm(){
				let _that = this
				for(let index in _that.$refs.autoform.formdata){
					if(index.includes("month") && index.includes("Name")){
						let oIndex = index.substr(0,(index.length-4))
						if(_that.$refs.autoform.formdata[oIndex+'Code']){
							_that.$refs.autoform.$set(_that.$refs.autoform.formdata,index,_that.$refs.autoform.formdata[oIndex+'Code']+_that.$refs.autoform.formdata.indexUnitName)
							_that.$set(_that.formdata,index,_that.$refs.autoform.formdata[oIndex+'Code']+_that.$refs.autoform.formdata.indexUnitName)
						}
					}
					if(index.includes("quarter") && index.includes("Name")){
						let oIndex = index.substr(0,(index.length-4))
						if(_that.$refs.autoform.formdata[oIndex+'Code']){
							_that.$refs.autoform.$set(_that.$refs.autoform.formdata,index,_that.$refs.autoform.formdata[oIndex+'Code']+_that.$refs.autoform.formdata.indexUnitName)
							_that.$set(_that.formdata,index,_that.$refs.autoform.formdata[oIndex+'Code']+_that.$refs.autoform.formdata.indexUnitName)
						}
					}
				}
				return true
			},
			initLoad(){
				let _that = this
				let arr = [
					_that.om.request(
						"torgAnnualTargetPlan/getCheckList"
					)
				]
				Promise.all(arr).then(res=>{
					_that.$refs.autoform.changeEditor("title_monthIndex","checksId","data",configSelectData(res[0],"id","text"))
					_that.$refs.autoform.initSelectIndex("title_monthIndex","checksId")
				})
			}
		},
		onShow() {
			let _that = this
			if(_that.$refs.autoform){
				uni.getStorage({
					key:_that.indexCache, 
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.orderNum = d.orderNum
							formdata.indexName = d.indexName
							formdata.indexId = d.indexId
							_that.verifyQuarter(d.indexId)
							formdata.busRange = d.busRange
							formdata.indexUnitName = d.indexUnitName
							formdata.targetValueName = d.targetValueName|| ''
							formdata.inputPrecision = d.inputPrecision
							formdata.decomposeTypeName = d.decomposeTypeName|| ''
							if(d.challengeTargetCode){
								formdata.challengeTargetName = (d.challengeTargetCode || '') + d.indexUnitName
							}
							_that.$refs.autoform.changeEditor("title_monthIndex","checksId",'type',"selectbox")
							for(let item of _that.$refs.autoform.configData[0].editors){
								if(item.valueField.includes("month") || item.valueField.includes("quarter")){
									_that.$refs.autoform.changeEditor("title_monthIndex",item.valueField,"type","numberbox")
									_that.$refs.autoform.changeEditor("title_monthIndex",item.valueField,"prompt","请输入")
									_that.$refs.autoform.changeEditor("title_monthIndex",item.valueField,"suffix",d.indexUnitName)
									_that.$refs.autoform.changeEditor("title_monthIndex",item.valueField,"suffixCol",d.indexUnitName.length)
								}
							}
							uni.removeStorage({
								key:_that.indexCache
							})
						}
					}
				})
			}
		},
		onLoad(e) {
			let _that = this
			_that.$nextTick(function(){
				subOnLoad(_that, e, function() {
				if(e.configDataCacheKey){
					_that.indexList = JSON.parse(e.indexList)
					for(let dIndex in _that.datas){
						if(dIndex != _that.index){
							for(let index in _that.indexList){
								if(_that.indexList[index].indexId == _that.datas[dIndex].indexId){
									_that.indexList.splice(index,1)
								}
							}
						}
					}
					uni.getStorage({
						key: e.configDataCacheKey,
						success: function(res) {
							if(res.data && Object.prototype.toString.call(res.data)==='[object Array]'){
								_that.autoformdata = res.data
								Object.assign(_that.initValue, _that.formdata)
								_that.autoformdata[0].order = _that.order
								if(!_that.formdata.orderNum){
									changeEditor("title_monthIndex","checksId",'type',"labelbox",_that.autoformdata)
								}else{
									for(let item of _that.autoformdata[0].editors){
										if(item.valueField.includes("month") || item.valueField.includes("quarter")){
											changeEditor("title_monthIndex",item.valueField,"type","numberbox",_that.autoformdata)
											changeEditor("title_monthIndex",item.valueField,"prompt","请输入",_that.autoformdata)
											if(_that.formdata.indexUnitName){
												changeEditor("title_monthIndex",item.valueField,"suffix",_that.formdata.indexUnitName,_that.autoformdata)
												changeEditor("title_monthIndex",item.valueField,"suffixCol",_that.formdata.indexUnitName.length,_that.autoformdata)
											}
										}
									}
								}
								uni.setStorage({
									key: _that.index1Cache,
									data:_that.indexList,
									success: (res) => {
										let title = getEditorTitle("title_monthIndex", "orderNum",_that.autoformdata)
										changeEditor("title_monthIndex", "orderNum", {
											link: "../../public/search/search",
											"linkParam": {
												other: JSON.stringify({}),
												cachename: _that.indexCache,
												otherCache: _that.index1Cache,
												defaultData: true,
												url:'',
												initload: true,
												namefield: "orderNum",
												extrafield: 'indexName',
												placeholder: "请输入" + title
											}
										},null,_that.autoformdata)
									}
								})
									_that.initLoad()
									_that.$nextTick(function(){
										
									})
								}
							}
						})
					}
								
				})
			})
		}
	}
</script>

<style>
</style>                                                                                                                                                                                                                                                                                                                                                                                                                                                         
